﻿using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using SPW.Infrastructure.Interfaces;
using SPW.Infrastructure.Utils;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using SyntacticSugar;
using Prism.Interactivity.InteractionRequest;

namespace SPW.LogIn.ViewModels
{
    public class DefaultLoginViewModel : BindableBase, IInteractionRequestAware
    {
        private string _title;

        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        private string _errorMessage;
        public string ErrorMessage
        {
            get { return _errorMessage; }
            set { SetProperty(ref _errorMessage, value); }
        }

        private bool _isError = false;
        public bool IsError
        {
            get { return _isError; }
            set { SetProperty(ref _isError, value); }
        }


        private string _userName;
        public string UserName
        {
            get => _userName;
            set => SetProperty(ref _userName, value);
        }

        private string _password;
        public string Password
        {
            get => _password;
            set => SetProperty(ref _password, value);
        }

        private bool _isFullScreen = true;
        public bool IsFullScreen
        {
            get => _isFullScreen;
            set => SetProperty(ref _isFullScreen, value);
        }

        private bool _asycnLogIn = false;

        public bool AsycnLogIn
        {
            get => _asycnLogIn;
            set => SetProperty(ref _asycnLogIn, value);
        }

        public INotification Notification { get; set; }
        public Action FinishInteraction { get; set; }

        private readonly IRegionManager _regionManager;
        private readonly IEventAggregator _eventAggregator;
        private readonly IDataServiceClient _dataServiceClient;
        private readonly IRunSettings _runSettings;

        public DefaultLoginViewModel(IRegionManager regionManager,
            IEventAggregator eventAggregator,
            IDataServiceClient dataServiceClient,
            IRunSettings runSettings)
        {
            _regionManager = regionManager;
            _eventAggregator = eventAggregator;
            _dataServiceClient = dataServiceClient;
            _runSettings = runSettings;
            Title = _runSettings.SoftwareSubtitle;
        }

        public ICommand LogInCompleteCommand
        {
            get => new DelegateCommand<FrameworkElement>(async (ui) =>
            {
                if (UserName.IsNullOrEmpty() || Password.IsNullOrEmpty())
                {
                    IsError = true;
                    ErrorMessage = "请输入账号或密码";
                    return;
                }

                ui.IsEnabled = false;
                AsycnLogIn = true;

                ErrorMessage = await _dataServiceClient.AuthenticatorAsync(UserName, Password).ContinueWith(r =>
                {
                    if (r.Exception.IsValuable()) return r.Exception.InnerException.Message;
                    return "";
                });

                if (ErrorMessage.IsNullOrEmpty())
                {
                    IsError = false;
                    this._eventAggregator.GetEvent<Infrastructure.Events.LogInCompletedEvent>().Publish(new Infrastructure.Events.LogInCompletedEvent(true, IsFullScreen));
                    FinishInteraction();
                }
                else
                {
                    IsError = true;
                    ui.IsEnabled = true;
                    AsycnLogIn = false;
                }

                //if (await _dataServiceClient.AuthenticatorAsync(UserName, Password))
                //{
                //    //var p = new Dictionary<string, string>() { { "page", "1" }, { "limit", "10" } };
                //    //var r = await _dataServiceClient.HttpGetAsync<object>("/api/ApplicationSystem/GetPageList", p);

                //    //_regionManager.RequestNavigate("ContentRegion", "defaultLayout");
                //    this._eventAggregator.GetEvent<Infrastructure.Events.LogInCompletedEvent>().Publish(true);
                //}
                //else
                //{
                //    ErrorMessage = "账号或密码不正确，请检查！";
                //    ui.IsEnabled = true;
                //    AsycnLogIn = false;
                //}
            });
        }

        public ICommand ExitApplicationCommand
        {
            get => new DelegateCommand(() =>
            {
                Process.GetCurrentProcess().Kill();
            });
        }

        public ICommand CloseCommand { get; set; }
    }
}
